package com.demo2do.fms.admin.web.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.fms.admin.service.fileupload.FileUploadService;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
public class FileUploadController {
    
    private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class);
    
    @Autowired
    private FileUploadService fileUploadService;
    
    /**
     * 
     * @param fileObject
     * @param objectId
     * @param fileName
     * @param filePath
     * @param dateName
     * @param dateValue
     * @return
     */
    @RequestMapping(value = "/fileupload-callback", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody String onFileUploadCallback(@RequestParam("fileObject") String fileObject,
                                                     @RequestParam("objectId") Long objectId,
                                                     @RequestParam("fileName") String fileName,
                                                     @RequestParam("filePath") String filePath,
                                                     @RequestParam(value = "dateName", required = false) String dateName,
                                                     @RequestParam(value = "dateValue", required = false) Date dateValue) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            fileUploadService.saveFile(fileObject, objectId, fileName, filePath, dateName, dateValue);
            httpResult.success().message("保存文件成功");
            
        } catch (Exception e) {
            logger.error("onFileUploadCallback save file faild", e);
            httpResult.success().message("保存文件失败");
        }
        
        return JsonUtils.toJson(httpResult);
        
    }
    
}
